<template>
    <div>
        <el-breadcrumb separator-class="el-icon-arrow-right">
            <el-breadcrumb-item :to="{ path: '/home' }">
                首页</el-breadcrumb-item
            >
            <el-breadcrumb-item :to="{ path: '/banner/List' }">
                内容管理</el-breadcrumb-item
            >
            <el-breadcrumb-item :to="{ path: '/article_cate/List' }">
                文章分类管理</el-breadcrumb-item
            >
            <el-breadcrumb-item>添加文章分类</el-breadcrumb-item>
        </el-breadcrumb>
        <el-card class="box-card">
            <el-page-header @back="goBack" content="添加文章分类">
            </el-page-header>
            <el-form
                :model="addForm"
                :rules="addFormRules"
                ref="addFormRef"
                label-width="120px"
                hide-required-asterisk
            >
                <el-form-item label="父级分类" align="left">
                    <el-tooltip
                        :enterable="false"
                        effect="dark"
                        content="不选择则分类为顶级分类"
                        placement="top"
                    >
                        <select-tree
                            ref="setTree"
                            @subSelectData="setParentId"
                        ></select-tree>
                    </el-tooltip>
                </el-form-item>
                <el-form-item label="分类名称" prop="name">
                    <el-input
                        v-model="addForm.name"
                        placeholder="请输入分类名称"
                    >
                    </el-input>
                </el-form-item>
                <el-form-item label="分类模板名称" prop="template_name">
                    <el-input
                        v-model="addForm.template_name"
                        placeholder="请输入文章分类模板名称"
                    />
                </el-form-item>

                <el-form-item label="备注">
                    <el-input
                        type="textarea"
                        autosize
                        v-model="addForm.remarks"
                        placeholder="请输入内容"
                    >
                    </el-input>
                </el-form-item>
                <el-form-item label="排序" prop="sort">
                    <el-input v-model="addForm.sort" placeholder="请输入排序" />
                </el-form-item>
                <el-form-item label="状态" align="left">
                    <el-switch
                        v-model="addForm.status"
                        :active-value="1"
                        :inactive-value="0"
                    ></el-switch>
                </el-form-item>
                <el-form-item label="文章内容" align="left">
                    <quill-editor
                        class="quill-editor ql-editor"
                        v-model="content"
                        ref="myQuillEditor"
                        :options="editorOption"
                        @blur="onEditorBlur($event)"
                        @focus="onEditorFocus($event)"
                        @change="onEditorChange($event)"
                    >
                    </quill-editor>
                </el-form-item>
                <el-form-item align="left">
                    <el-button type="primary" @click="subAdd">保存</el-button>
                    <el-button @click="resetForm">重置</el-button>
                </el-form-item>
            </el-form>
        </el-card>
    </div>
</template>

<script>
import requestApis from '@/request'
import SelectTree from '@/components/select_tree/SelectTree.vue'
export default {
    components: { SelectTree },
    data () {
        return {
            addForm: {
                parent_id: 0,
                name: '',
                template_name: '',
                remarks: '',
                sort: 10,
                status: 1
            },
            content: '', // 保存富文本框的内容和图片
            // 添加表单验证规则对象 tag value_type
            addFormRules: {
                name: [
                    { required: true, message: '请输入栏目名称', trigger: 'change' }
                ],
                template_name: [
                    { required: true, message: '请选择栏目类型', trigger: 'blur' }
                ]
            },
            editorOption: { // 配置富文本需要的功能
                ['bold', 'italic', 'underline', 'strike'],        // toggled buttons
                ['blockquote', 'code-block'],

                [{ 'header': 1 }, { 'header': 2 }],               // custom button values
                [{ 'list': 'ordered' }, { 'list': 'bullet' }],
                [{ 'script': 'sub' }, { 'script': 'super' }],      // superscript/subscript
                [{ 'indent': '-1' }, { 'indent': '+1' }],          // outdent/indent
                [{ 'direction': 'rtl' }],                         // text direction

                [{ 'size': ['small', false, 'large', 'huge'] }],  // custom dropdown
                [{ 'header': [1, 2, 3, 4, 5, 6, false] }],

                [{ 'color': [] }, { 'background': [] }],          // dropdown with defaults from theme
                [{ 'font': [] }],
                [{ 'align': [] }],
                ['link', 'image', 'video'],
                ['clean']                 
                }
        }
    }
},
mounted() {
    this.getArticleCategoryTree()
},
methods: {
    setParentId(parentId) {
        this.addForm.parent_id = parentId
    },
    // 准备编辑器
    onEditorReady(editor) {
    },
    onEditorBlur() { }, // 失去焦点事件
    onEditorFocus() { }, // 获得焦点事件
    onEditorChange(event) {
        event.quill.deleteText(100, 4) // 限制输入字数，行数。
    }, // 内容改变事件
    getArticleCategoryTree() {
        requestApis.getArticleCategoryTree().then(res => {
            if (res.meta.code === 1) {
                this.$refs.setTree.setSelectData(res.data)
            } else {
                this.mg({
                    message: res.meta.msg,
                    type: 'warning'
                })
            }
        }).catch(error => {
            console.log(error)
        })
    },
    goBack() {
        this.$router.back()
    },
    subAdd() {
        this.$refs.addFormRef.validate(valid => {
            if (!valid) {
                return false
            }
            requestApis.saveArticleCategoryAdd(this.addForm).then(res => {
                if (res.meta.code === 1) {
                    this.$message({
                        message: res.meta.msg,
                        type: 'success'
                    })
                    this.$router.back()
                } else {
                    this.$message({
                        message: res.meta.msg,
                        type: 'warning'
                    })
                }
            }).catch(error => {
                console.log(error)
            })
        })
    },
    resetForm() {
        this.$refs.addFormRef.resetFields()
        this.addForm.status = 1
    }
}
}
</script>

<style>
.el-form {
    width: 60%;
    margin-top: 20px;
}
</style>
